package com.bosssoft.springboot.demo4.service.impl;

import com.bosssoft.springboot.demo4.service.EMailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

/**
 * @description email接口实现类 实现具体的email操作
 * @author ChenTong
 * @date 2020-6-10
 */
@Slf4j
@Service
public class EMailServiceImpl implements EMailService {
    @Autowired
    private JavaMailSender mailSender;

    /**
     * @description 使用spring-boot-mail-starter进行邮件的发送
     * @param from 发送方
     * @param to 接收方
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    @Override
    public void send(String from,String to, String subject, String content) {
        //设置邮件具体内容
        SimpleMailMessage mailMessage=new SimpleMailMessage();
        mailMessage.setFrom(from);
        mailMessage.setTo(to);
        mailMessage.setSubject(subject);
        mailMessage.setText(content);
        //发送邮件
        try {
            mailSender.send(mailMessage);
            log.info("发送成功");
        }catch (Exception e){
            log.error("发送简单邮件失败",e);
        }
    }
}
